package com.boco.transnms.server.dao.traph;

import com.boco.transnms.server.dao.base.AbstractDAO;

import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.DdfCheckTask;
import com.boco.transnms.common.dto.QueryDDFCheckTaskDO;
import com.boco.transnms.common.dto.DdfCheckResult;
import com.boco.transnms.common.dto.DdfCheckDetailresult;
import com.boco.transnms.server.dao.base.DaoName;
import com.boco.transnms.common.dto.base.BoQueryContext;
import com.boco.transnms.common.dto.base.DboCollection;
import com.boco.transnms.server.dao.base.DaoHelper;
import com.boco.common.util.db.DbConnManager;
import com.boco.common.util.db.DbType;
import com.boco.common.util.db.SqlHelper;
import com.boco.common.util.lang.TimeFormatHelper;

import com.boco.transnms.common.dto.Traph;
import com.boco.transnms.server.dao.base.ObjectGetTypeEnum;
import com.boco.transnms.common.dto.base.DataObjectList;

/**
 * <p>Title: TransNMS</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Boco Inter-Telecom INC</p>
 *
 * @author not attributable
 * @version 1.0
 */  
public class TraphDDFPortsCheckDAO extends AbstractDAO{
    public TraphDDFPortsCheckDAO() {
        super(DaoName.TraphDDFPortsCheckDAO);
    }

    public DdfCheckTask addTask(BoActionContext actionContext, DdfCheckTask ddfCheckTask) throws Exception {
        super.createObject(actionContext,ddfCheckTask);
        return ddfCheckTask;
    }

    public void deleteTask(BoActionContext actionContext, DdfCheckTask ddfCheckTask) throws Exception {
        super.deleteObject(actionContext,ddfCheckTask);
    }

    public DdfCheckTask modifyTask(BoActionContext actionContext, DdfCheckTask ddfCheckTask) throws Exception {
        super.updateObject(actionContext,ddfCheckTask);
        return ddfCheckTask;
    }

    public DboCollection getTaskByDo(BoQueryContext queryContext, QueryDDFCheckTaskDO queryDDFCheckTaskDO) throws Exception {
        String taskName = queryDDFCheckTaskDO.getTaskName();
        String taskState = queryDDFCheckTaskDO.getTaskState();
        String user = queryDDFCheckTaskDO.getUser();
        String beginTime = queryDDFCheckTaskDO.getCheckBeginTime();
        String endTime = queryDDFCheckTaskDO.getCheckEndTime();
        String orderString = queryDDFCheckTaskDO.getOrderFieldString();
        StringBuffer sb = new StringBuffer("SELECT * FROM DDF_CHECK_TASK WHERE 1=1");
        if(DaoHelper.isNotEmpty(taskName)){
            sb.append(" AND " + DdfCheckTask.AttrName.taskName + " LIKE '%"+taskName+"%' ");
        }
        if(DaoHelper.isNotEmpty(taskState)&&!taskState.equals("0")){
            sb.append(" AND " + DdfCheckTask.AttrName.taskState + "=" + taskState + " ");
        }
        if(DaoHelper.isNotEmpty(user)&&!user.equals("0")){
            sb.append(" AND " + DdfCheckTask.AttrName.createUser + " LIKE '%" +user+ "%' ");
        }
        DbType dbType = DbConnManager.getInstance().getDbContext().getDbType();
        if(DaoHelper.isNotEmpty(beginTime)){
            sb.append(" AND " + DdfCheckTask.AttrName.createtime + ">=" + SqlHelper.getTimestamp(dbType, TimeFormatHelper.getFormatTimestamp(beginTime + ".000")));
        }
        if(DaoHelper.isNotEmpty(endTime)){
            sb.append(" AND " + DdfCheckTask.AttrName.checkendTime + "<=" + SqlHelper.getTimestamp(dbType, TimeFormatHelper.getFormatTimestamp(endTime + ".000")));
        }
        if(DaoHelper.isNotEmpty(orderString)){
            sb.append(" ORDER BY " + orderString);
        }
        String sql = sb.toString();
        return super.selectDBOs(queryContext, sql, new DdfCheckTask());
    }

    public DdfCheckResult addTaskResult(BoActionContext actionContext, DdfCheckResult ddfCheckResult) throws Exception {
        super.createObject(actionContext,ddfCheckResult);
        return ddfCheckResult;
    }

    public DboCollection getTaskResult(BoQueryContext queryContext, String taskCuids, String orderString) throws Exception {
        taskCuids = "'" + taskCuids.replaceAll(",","','") + "'";
        String sql = "SELECT D.*,T.NO,T.LABEL_CN FROM DDF_CHECK_RESULT D,TRAPH T WHERE " + DdfCheckResult.AttrName.relatedTaskCuid + " IN(" + taskCuids + ")"+
                     " AND D." + DdfCheckResult.AttrName.relatedTraphCuid + "=T." + Traph.AttrName.cuid;
        if(DaoHelper.isNotEmpty(orderString)){
            if(orderString.indexOf(Traph.AttrName.no)==0||orderString.indexOf(Traph.AttrName.labelCn)==0){
                sql += " ORDER BY T." + orderString;
            }else{
                sql += " ORDER BY D." + orderString;
            }
        }
        return super.selectDBOs(queryContext, sql, new DdfCheckResult(), new Traph());
    }


    public DdfCheckDetailresult addTaskDetailResult(BoActionContext actionContext, DdfCheckDetailresult ddfCheckDetailResult) throws Exception {
        super.createObject(actionContext,ddfCheckDetailResult);
        return ddfCheckDetailResult;
    }

    public DboCollection getTaskDetailResult(BoQueryContext queryContext, String taskCuids, String orderString) throws Exception {
        taskCuids = "'" + taskCuids.replaceAll(",","','") + "'";
        String sql = "SELECT D.* FROM DDF_CHECK_DETAILRESULT D,TRAPH T WHERE " + DdfCheckDetailresult.AttrName.relatedTaskCuid + " IN(" + taskCuids + ")"+
                     " AND D." + DdfCheckResult.AttrName.relatedTraphCuid + "=T." + Traph.AttrName.cuid;
        if(DaoHelper.isNotEmpty(orderString)){
            sql += " ORDER BY " + orderString;
        }
        return super.selectDBOs(queryContext, sql, new DdfCheckDetailresult());
    }

    public void delCheckTask(BoActionContext actionContext, String ddfCheckTaskCuid) throws Exception {
        DboCollection dbos = getDdfCheckDetailresultByTaskCuid(actionContext,ddfCheckTaskCuid);
        DataObjectList list = new DataObjectList();
        if(null != dbos && dbos.size() > 0){
            for(int i = 0; i < dbos.size(); i++){
                DdfCheckDetailresult ddfCheckDetailresult = (DdfCheckDetailresult)dbos.getAttrField(DdfCheckDetailresult.CLASS_NAME,i);
                list.add(ddfCheckDetailresult);
            }
            super.deleteObjects(actionContext, list);
        }
        dbos = getTaskResultByTaskCuid(actionContext,ddfCheckTaskCuid);
        if(null != dbos && dbos.size() > 0){
            list = new DataObjectList();
            for(int i = 0; i < dbos.size(); i++){
                DdfCheckResult ddfCheckResult = (DdfCheckResult)dbos.getAttrField(DdfCheckResult.CLASS_NAME,i);
                list.add(ddfCheckResult);
            }
            super.deleteObjects(actionContext, list);
        }
        delTask(actionContext,ddfCheckTaskCuid);
    }

    public void delTaskDetailResultByTaskResultCuids(BoActionContext actionContext, String taskResultCuids) throws Exception {
        if(DaoHelper.isNotEmpty(taskResultCuids)){
            taskResultCuids = taskResultCuids.replaceAll(",","','");
            taskResultCuids = "'"+taskResultCuids+"'";
            String sql ="DELETE FROM " + DdfCheckDetailresult.CLASS_NAME + " WHERE " + DdfCheckDetailresult.AttrName.relatedTaskCuid + " IN ("+ taskResultCuids+")";
            super.execSql(sql);
        }
    }

    public void delTask(BoActionContext actionContext, String checkTaskCuid) throws Exception {
        if(DaoHelper.isNotEmpty(checkTaskCuid)){
            checkTaskCuid = checkTaskCuid.replaceAll(",","','");
            checkTaskCuid = "'"+checkTaskCuid+"'";
            String sql ="DELETE FROM " + DdfCheckTask.CLASS_NAME + " WHERE " + DdfCheckTask.AttrName.cuid + " IN ("+ checkTaskCuid+")";
            super.execSql(sql);
        }
    }

    public DboCollection getDdfCheckDetailresultByTaskCuid(BoActionContext actionContext, String ddfCheckTaskCuid) throws Exception {
        String sql = "SELECT D.* FROM DDF_CHECK_DETAILRESULT D,DDF_CHECK_RESULT R, DDF_CHECK_TASK T WHERE " +
                     " T." + DdfCheckTask.AttrName.cuid + "=R." + DdfCheckResult.AttrName.relatedTaskCuid +
                     " AND R." + DdfCheckResult.AttrName.cuid + "=D." + DdfCheckDetailresult.AttrName.relatedTaskCuid +
                     " AND T." + DdfCheckTask.AttrName.cuid + "='" + ddfCheckTaskCuid+"'";
        return super.selectDBOs(sql, new DdfCheckDetailresult());
    }

    public DboCollection getTaskResultByTaskCuid(BoActionContext actionContext, String ddfCheckTaskCuid) throws Exception {
        String sql = "SELECT R.* FROM DDF_CHECK_RESULT R, DDF_CHECK_TASK T WHERE " +
                     " T." + DdfCheckTask.AttrName.cuid + "=R." + DdfCheckResult.AttrName.relatedTaskCuid +
                     " AND T." + DdfCheckTask.AttrName.cuid + "='" + ddfCheckTaskCuid+"'";
        return super.selectDBOs(sql, new DdfCheckResult());
    }

    public DboCollection getDetailDDFResultToExcel(String taskCuid) throws Exception {
        String sql = "SELECT R.*,D.*,P.NO,P.LABEL_CN FROM DDF_CHECK_TASK T,DDF_CHECK_RESULT R,DDF_CHECK_DETAILRESULT D,TRAPH P WHERE T.CUID=R.RELATED_TASK_CUID AND R.CUID = D.RELATED_TASK_CUID AND D.RELATED_TRAPH_CUID = P.CUID AND T.CUID ='"+taskCuid+"'";
        return super.selectDBOs(sql, new DdfCheckResult(), new DdfCheckDetailresult(), new Traph());
    }


    public String getTraphObjectIdsByTask(DdfCheckTask DdfCheckTask)throws Exception {
        String sql = "SELECT DISTINCT(T.OBJECTID) FROM DDF_CHECK_RESULT R,TRAPH T WHERE R.RELATED_TRAPH_CUID=T.CUID AND R.RELATED_TASK_CUID='"+DdfCheckTask.getCuid()+"'";
        DataObjectList traphObjList = super.selectDBOs(sql, new Class[] {String.class});
        String objIds = "";
        if (null != traphObjList && traphObjList.size() > 0) {
            for (int i = 0; i < traphObjList.size(); i++) {
                String objId = traphObjList.get(i).getAttrString("1");
                if (objIds.trim().length() == 0) {
                    objIds = objId;
                } else {
                    objIds = objIds + "," + objId;
                }
            }
        }
        return objIds;
    }

    public DataObjectList getAllDdfCheckTask(BoActionContext actionContext) throws Exception {
          return super.getAllObjByClass(new DdfCheckTask(),  ObjectGetTypeEnum.FULL);
   }
}
